#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@file        :News.py
@Description:       :
@Date     :2021/08/13 16:24:36
@Author      :mwx
@version      :1.0
@Contact      : 1361832073@qq.com
'''

import csv 
import requests
import datetime
import time
#热门项目新闻
def new():
    url = "https://api.cntv.cn/newList/getMixListByPageId"
    sportId = {
        '东京日记':'PAGE0s181amoEsL3mDpxnxWY210702',
        '大咖陪你看':'PAGEKiBpOmwxeWj4r4s1IId4210702',
        '荣誉殿堂':'PAGEry6q2BAUwBtNlH1W4IB5210720',
        '体坛英豪':'PAGEatKpM7E3ByupdTfOeDIj210702',
        '人在奥运年':'PAGEvdfr9pIBYqfG1dB7ocpR210706'
    }
    
    params = {
        'serviceId' : 'pcsports',
        'id' : '',
        'catg' : '1'
    }
    list = ['TokyoDiary','ComeWithYouWatch','HallOfHonor','Sportsman','OlympicYear']
    #获取5个栏目的奥运相关新闻
    fatherList = []
    for i in sportId:
        params['id'] = sportId[i] #拼接参数
        json = requests.get(url,params=params).json()
        result = json['data']['list']
        templist = []
        fatherList.append(templist) 
        #爬取该栏目下的新闻
        for item in result:
            items = []
            items.append(item['title'])
            items.append(item['url'])
            templist.append(items)
    
    #遍历
    for i in range(len(list)):
        list[i]  = fatherList[i]     
    
    #保存
    time=datetime.datetime.now()
    # filepath = "../OlpApp/static/data/News"+str(time).split(' ')[0]+".csv"
    # with open(filepath,'wt',encoding='utf-8') as f:
    #     cw = csv.writer(f, lineterminator = '\n')
    #     for i in range(len(list)):
    #         for item in list[i]:
    #             cw.writerow(item)
    for i in range(len(list)):
        filepath = "../OlpApp/static/data/News" +str(i)+ str(time).split(' ')[0] + ".csv"
        with open(filepath, 'wt', encoding='utf-8') as f:
            cw = csv.writer(f, lineterminator='\n')
            for item in list[i]:
                cw.writerow(item)
    return list[0],list[1],list[2],list[3],list[4]

#   设置定时任务
def main(h,m):
    while True:
        now = datetime.datetime.now()
        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        print(now.hour, now.minute)
        if now.hour == h and now.minute in m:
            list1,list2,list3,list4,list5 = new()
            print(list1)
        time.sleep(60)

if __name__ == "__main__":
    # new()
    main(1,0)

